package com.ruoyi.rsikManage.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.rsikManage.domain.VideoResourceTreeNode;
import com.ruoyi.rsikManage.service.MonitorPointService;
import com.ruoyi.rsikManage.service.VideoResourceTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/videoResourceTree")
@RestController
public class VideoResourceTreeController extends BaseController {
    @Autowired
    VideoResourceTreeService videoResourceTreeService;
    @Autowired
    MonitorPointService monitorPointService;
    /**
     * 查询所有资源树
     */
    @GetMapping("/selectAll")
    public AjaxResult selectVideoResourceTree(String treeType) {
        List<VideoResourceTreeNode> treeNodeList;
        treeNodeList = videoResourceTreeService.selectTreeByTreeType(treeType);
        return AjaxResult.success(treeNodeList);
    }


    @GetMapping(value = "/{nodeId}")
    public AjaxResult getInfo(@PathVariable("nodeId") Integer nodeId)
    {
        VideoResourceTreeNode videoResourceTreeNode = videoResourceTreeService.selectTreeNodeById(nodeId);
        System.out.println(videoResourceTreeNode);
        return AjaxResult.success(videoResourceTreeNode);
    }

    /**
     * 新增资源树结点
     */
    @PostMapping("/addNode")
    public AjaxResult addVideoResourceTreeNode(@RequestBody VideoResourceTreeNode treeNode) {
        if (!videoResourceTreeService.checkTreeNodeNameUnique(treeNode))
        {
            return error("新增结点'" + treeNode.getNodeName() + "'失败，结点名称已存在");
        }
        return toAjax(videoResourceTreeService.insertTreeNode(treeNode));
    }

    /**
     * 修改资源树结点
     */
    @PutMapping("/updateNode")
    public AjaxResult updateVideoResourceTreeNode(@RequestBody VideoResourceTreeNode treeNode) {
        return AjaxResult.success(videoResourceTreeService.updateTreeNode(treeNode));
    }

    /**
     * 删除资源树结点
     */
    @DeleteMapping("/deleteNode/{nodeId}")
    @Transactional
    public AjaxResult deleteTreeNodeRelMonitor(@PathVariable Integer nodeId) {
        monitorPointService.deleteTheTreeNodeAndAllMonitorsRel(nodeId);
        return AjaxResult.success(videoResourceTreeService.deleteTreeNodeByNodeId(nodeId));
    }

    /**
     * 查询资源树有几种类型
     */
    @GetMapping("/selectTreeType")
    public AjaxResult selectTreeType() {

        List<String> treeTypeList = videoResourceTreeService.selectAllTreeType();
        return AjaxResult.success(treeTypeList);
    }
//    @DeleteMapping("deleteNode/{ids}")
//    public AjaxResult deleteVideoDiagnosisResultByIds(@PathVariable Integer[] ids){
//        return toAjax(videoResourceTreeService.deleteTreeNodeByNodeIds(ids));
//    }

}
